﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace StateManager
{
    /// <summary>
    /// Base class for all vehicles
    /// </summary>
    abstract class Vechicle
    {
        // this stores the image of the vehicle
        protected Texture2D tex;

       

        // this is used to store location of the object on screen as X and Y co-ordinates
        protected Vector2 pos;
        // used to set speed of vehicle
        protected Vector2 vel;

        public Vector2 Vel
        {
            get { return vel; }
        }

        // this rectangle is a property which returns a new rectangle when called
        

        public Direction dir;
        // this stores the direction the vehicle moves on screen

        protected Boolean isWaiting;
        // this boolean is set and checked to see if the vehicle is set or is waiting

        public virtual void loadContent(Level lvl) { }
        public virtual void Update(GameTime gameTime, Rectangle viewPort) { }
        public virtual void Draw(SpriteBatch spriteBatch, GameTime gameTime) { }

        // abstract methods that must be included in any inherited class
    }

    // this is an enumerator
    // this enumerator is used to define what direction a vehicle or log is moving
    enum Direction
    {
        Left = -1,
        Right = 1
    }

    class moveObject : Vechicle
    {
        // this floating point number is used to store the amount of time
        // the vehicle has to wait until it can appear again on the screen
        // the value represents miliseconds
        protected float timeWait;

        protected float timeTaken = 0;

        protected Random random;

        protected int pace = 0;

        // this object stores a font which allows text to be drawn on screen
        protected SpriteFont sFont;

        public virtual Rectangle colRec
        {
            get { return new Rectangle((int)pos.X, (int)pos.Y, tex.Width, tex.Height); }
            // a rectangle requires four variables:
            // the x co-ordinates of the object
            // the y co-ordinates of the object
            // the width of the texture used by the object (in pixels)
            // the height of the textured used by the object
        }

        public moveObject(Direction facing, float time, float x, float y, float velx, int pace)
        {

            vel = new Vector2(velx, 0);

            dir = facing;

            isWaiting = true;
            

            this.pace = pace;
            //timeWait = time * velx;

            //setStartingPoint();
        }

        public virtual void doSomething()
        {
        }

        // the update method for the vechicle class, this is where all of the logic for the vehicles
        // is performed
        public override void Update(GameTime gameTime, Rectangle viewPort)
        {
            // timetaken is used to store the elapsed time taken from the gameTime object
            // and is compared to the timewaiting variable

            if (!isWaiting) // if the truck isn't waiting
            {
                // then the position on the x axis is increased (or decreased) depending on the enum stored
                switch (dir)
                {
                    case Direction.Left:
                        pos -= vel;
                        break;
                    case Direction.Right:
                        pos += vel;
                        break;
                    default:
                        break;
                }


                if (!viewPort.Intersects(colRec)) // if the truck is no longer on screen
                {
                    // it is set to wait
                    isWaiting = true;
                    // its position on the x axis is reset to its starting location
                    switch (dir)
                    {
                        case Direction.Left:
                            pos.X = viewPort.Right;
                            break;
                        case Direction.Right:
                            pos.X = viewPort.Left - colRec.Width;
                            break;
                        default:
                            break;
                    }
                }
            }
            else // it's waiting for the time given to wait is over
            {
                // the variable is assigned the elapsed game time from the the gameTime object
                // that is passed as a parameter..
                timeTaken += gameTime.ElapsedGameTime.Milliseconds;

                // ...and is checked to see if is equal or greater than the max amount of time to wait
                if (timeTaken >= timeWait)
                {
                    isWaiting = false; // if it is equal or greater, then its set to not wait (moving)
                    //timeTaken = 0;
                }
            }
        }

        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            //base.Draw(spriteBatch);
            if (dir == Direction.Left)
                //spriteBatch.Draw(tex, pos, null, Color.White, 0, Vector2.Zero, 0, SpriteEffects.FlipHorizontally, 0); // doesn't work
                spriteBatch.Draw(tex, colRec, null, Color.White, 0, Vector2.Zero, SpriteEffects.FlipHorizontally, 0);
            else
                spriteBatch.Draw(tex, pos, Color.White);  // the image for the truck is drawn on screen
        }

    }

    //class Truck : moveObject  // this class inherits from the vehicle class
    //{

      
    //}

    

    
}    